from __future__ import print_function

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.properties import NumericProperty, StringProperty
from goblinoid.comments.commentmodal import CommentModal
from goblinoid.root.profilelabel import ProfileLabel

Builder.load_file('goblinoid/feed/kv/commentsnapshot.kv')


class CommentSnapshot(ProfileLabel):
    pass


class ViewCommentsButton(Button):
    button_text = StringProperty()
    index = NumericProperty()
    count = NumericProperty()
    activity_id = StringProperty()

    def on_count(self, instance, count):
        if count == 0:
            self.text = 'no comments yet, be the first!'
        elif count == 1:
            self.text = 'view this comment'
        else:
            self.text = 'view all {0} comments'.format(count)

    def open_comment_modal(self):
        comment_modals = App.get_running_app().pump_service.comment_modals
        if self.activity_id not in comment_modals:
            activity_service = self.get_activity_service()
            comment_modal = CommentModal(activity_service=activity_service)
            App.get_running_app().pump_service.comment_modals[self.activity_id] = comment_modal
            comment_modal.open()
        else:
            cached_comment_modal = comment_modals[self.activity_id]
            cached_comment_modal.open()

    def get_activity_service(self):
        return App.get_running_app().pump_service.activity_manager.get_activity_service(
            self.activity_id)
